<template>
  <div>
    <el-button @click="copyTableData">复制到 Excel</el-button>
    <el-table :data="tableData" stripe>
      <el-table-column prop="name" label="姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref } from 'vue';

const tableData = ref([
  { name: '张三', age: 20 },
  { name: '李四', age: 25 }
]);

const copyTableData = async () => {
  let dataText = '姓名\t年龄\n';
  tableData.value.forEach((row) => {
    dataText += `${row.name}\t${row.age}\n`;
  });

  try {
    await navigator.clipboard.writeText(dataText);
    console.log('数据已复制到剪贴板');
  } catch (error) {
    console.error('复制数据到剪贴板时出错:', error);
  }
};
</script>